<?php

require_once 'vendor/autoload.php';

use Smalot\Cups\Builder\Builder;
use Smalot\Cups\Manager\JobManager;
use Smalot\Cups\Manager\PrinterManager;
use Smalot\Cups\Model\Job;
use Smalot\Cups\Transport\Client;
use Smalot\Cups\Transport\ResponseParser;
use Http\Client\Socket\Client as HttpClient;

/**
 * CUPS-IPP 本地打印测试示例
 * 
 * 使用前请确保：
 * 1. 已安装并启动 CUPS 服务
 * 2. 已配置至少一台打印机
 * 3. 已安装项目依赖 (composer install)
 */

try {
    echo "=== CUPS-IPP 本地打印测试 ===\n\n";
    
    // 1. 创建基础组件
    echo "1. 初始化 CUPS 客户端...\n";
    $builder = new Builder();
    $client = new Client();
    $httpClient = new HttpClient();
    $responseParser = new ResponseParser();
    
    // 2. 创建管理器
    $printerManager = new PrinterManager($builder, $httpClient, $responseParser);
    $jobManager = new JobManager($builder, $httpClient, $responseParser);
    
    echo "✓ 客户端初始化完成\n\n";
    
    // 3. 获取可用打印机列表
    echo "2. 获取可用打印机...\n";
    try {
        // 注意：这需要实际的CUPS服务器运行
        // $printers = $printerManager->getList('http://localhost:631/');
        // echo "找到 " . count($printers) . " 台打印机\n";
        
        // 由于可能没有运行的CUPS服务器，我们使用模拟数据
        echo "模拟打印机列表：\n";
        echo "- HP_LaserJet (ipp://localhost:631/printers/HP_LaserJet)\n";
        echo "- Canon_Printer (ipp://localhost:631/printers/Canon_Printer)\n";
        
    } catch (Exception $e) {
        echo "⚠️  无法连接到CUPS服务器: " . $e->getMessage() . "\n";
        echo "请确保CUPS服务正在运行 (通常在 http://localhost:631)\n";
    }
    
    echo "\n3. 创建打印任务...\n";
    
    // 4. 创建打印任务
    $job = new Job();
    $job->setName('测试打印任务');
    $job->setUsername('testuser');
    $job->setCopies(1);
    $job->setSides('one-sided');
    $job->setPrinterUri('ipp://localhost:631/printers/HP_LaserJet');
    
    // 添加要打印的内容
    $testContent = "这是一个测试打印文档\n\n" .
                   "CUPS-IPP PHP 库测试\n" .
                   "时间: " . date('Y-m-d H:i:s') . "\n" .
                   "\n" .
                   "如果您看到这个文档，说明打印功能正常工作！";
    
    $job->addText($testContent);
    
    echo "✓ 打印任务创建完成\n";
    echo "  - 任务名称: " . $job->getName() . "\n";
    echo "  - 用户名: " . $job->getUsername() . "\n";
    echo "  - 份数: " . $job->getCopies() . "\n";
    echo "  - 打印机URI: " . $job->getPrinterUri() . "\n";
    
    echo "\n4. 执行打印 (模拟)...\n";
    
    // 5. 实际打印 (需要真实的CUPS服务器)
    try {
        // 注意：这需要实际的CUPS服务器和打印机
        // $result = $jobManager->send($job);
        // echo "✓ 打印任务已发送，任务ID: " . $result->getId() . "\n";
        
        echo "模拟打印执行：\n";
        echo "✓ 打印任务已发送到打印机\n";
        echo "✓ 任务ID: 12345\n";
        echo "✓ 状态: 处理中\n";
        
    } catch (Exception $e) {
        echo "❌ 打印失败: " . $e->getMessage() . "\n";
    }
    
    echo "\n=== 测试完成 ===\n\n";
    
    // 6. 使用说明
    echo "📋 实际使用步骤：\n\n";
    echo "1. 安装 CUPS 服务器:\n";
    echo "   - Ubuntu/Debian: sudo apt-get install cups\n";
    echo "   - CentOS/RHEL: sudo yum install cups\n";
    echo "   - macOS: 系统自带\n";
    echo "   - Windows: 安装 CUPS for Windows\n\n";
    
    echo "2. 启动 CUPS 服务:\n";
    echo "   - Linux: sudo systemctl start cups\n";
    echo "   - macOS: sudo launchctl load /System/Library/LaunchDaemons/org.cups.cupsd.plist\n\n";
    
    echo "3. 配置打印机:\n";
    echo "   - 访问 http://localhost:631\n";
    echo "   - 添加打印机 (Administration > Add Printer)\n\n";
    
    echo "4. 修改代码中的打印机URI:\n";
    echo "   - 将 'ipp://localhost:631/printers/HP_LaserJet' 替换为实际的打印机URI\n\n";
    
    echo "5. 运行测试:\n";
    echo "   - php example_print_test.php\n\n";
    
} catch (Exception $e) {
    echo "❌ 错误: " . $e->getMessage() . "\n";
    echo "文件: " . $e->getFile() . ":" . $e->getLine() . "\n";
}